<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    /*添加评论API*/
    public function add(){
        if (!user_ins()->is_logged_in()){
            return ['status'=>0,'msg'=>'login require'];
        }
        if (!rq('content')){
            return ['status'=>0,'msg'=>'empty content'];
        }
        if (!rq('question_id') && !rq('answer_id') || rq('question_id') && rq('answer_id')){
            return ['status'=>0,'msg'=>'question_id or answer_id is require'];
        }
        if (rq('question_id')){
            $question = question_ins()->find(rq('question_id'));
            if (!$question){
                return ['status'=>0,'msg'=>'question not exists'];
            }
            $this->question_id = rq('question_id');

        }else{
            $answer = answers_ins()->find(rq('answer_id'));
            if (!$answer){
                return ['status'=>0,'msg'=>'answer not exists'];
            }
            $this->answers_id = rq('answer_id');
        }

        if (rq('reply_to')){
            $target = $this->find(rq('reply_to'));
            if (!$target){
                return ['status'=>0,'msg'=>'comment not exists'];
            }
            if ($target->user_id == session("user_id")){
                return ['status'=>0,'msg'=>'invalid reply_to'];
            }
            $this->reply_id = rq('reply_to');
        }

        $this->content = rq('content');
        $this->user_id = session('user_id');
        return $this->save()?
            ['status'=>1,'msg'=>'comment success']:
            ['status'=>0,'msg'=>'comment failed'];

    }
    /*查看评论API*/
    public function read(){
        if (!rq('answer_id') && !rq('question_id')){
            return ['status'=>0,'question_id or answer_id is require'];
        }
        if (rq('question_id')){
            $question = question_ins()->find(rq('question_id'));
            if (!$question){
                return ['status'=>0,'question not exists'];
            }
            $data = $this->where('question_id',rq('question_id'));
        }
        else{
            $answer = answers_ins()->find(rq('answer_id'));
            if (!$answer){
                return ['status'=>0,'answer not exists'];
            }
            $data = $this->where('answer_id',rq('answer_id'));
        }
        $data = $data->get()->keyBy('id');
        return ['status'=>1,'data'=>$data];
    }
    /*删除评论API*/
    public function remove(){
        if (!user_ins()->is_logged_in()){
            return ['status'=>0,'msg'=>'login require'];
        }

        if (!rq('id')){
            return ['status'=>0,'msg'=>'id is require'];
        }
        $comment = $this->find(rq('id'));
        if (!$comment) {
            return ['status'=>0,'msg'=>'comment not exists'];
        }
        if ($comment->user_id != session('user_id')){
            return ['status'=>0,'msg'=>'permission denied'];
        }
        $this->where('reply_id',rq('id'))->delete();
        return $comment->delete()?
            ['status'=>1,'msg'=>'success']:
            ['status'=>0,'msg'=>'delete failed'];
    }
}
